package com.system.data.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataConnection {
	
	private final String MYSQL_CONNECTION_STRING = "jdbc:mysql://localhost/%s";
	
	public Connection getConnection() throws ClassNotFoundException, SQLException{
		Connection conn = null;
		final String type = ConfigurationReader.getPropery(Constants.DB_TYPE);
		final String dbName = ConfigurationReader.getPropery(Constants.OWNED_DB_NAME_KEY);
		final String username = ConfigurationReader.getPropery(Constants.DB_USERNAME_KEY);
		final String password = ConfigurationReader.getPropery(Constants.DB_PASSWORD_KEY);
		if(Constants.MYSQL_TYPE.equalsIgnoreCase(type)){
			conn = getMySqlConnection(dbName, username, password);
		} 
		return conn;
	}
	
	private Connection getMySqlConnection(final String dbName, final String username, final String password) throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection(String.format(MYSQL_CONNECTION_STRING, dbName), username, password);
	}

}
